package com.segersten.othello.service.akka.lobby;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.segersten.othello.service.akka.Auth;
import com.segersten.othello.service.akka.Player;

public class LoginSessionRegistry
{
    private Map<DomainPlayer, Auth> registry = new HashMap<DomainPlayer, Auth>();
    private Map<Auth, DomainPlayer> auths = new HashMap<Auth, DomainPlayer>();

    public Auth addSession(String domain, String nick, Auth auth)
    {     
        DomainPlayer dp = new DomainPlayer(domain, nick);
        auths.put(auth, dp);
        Auth replaced = registry.put(dp, auth);
        if (replaced!=null) {
            auths.remove(replaced);
        }
        return replaced;
    }
    
    public boolean isAuthorized(Auth auth)
    {
        return auths.keySet().contains(auth);
    }

    public DomainPlayer getDomainPlayer(Auth auth)
    {
        return auths.get(auth);
    }

    public Set<Auth> getAllSessions()
    {
        return new HashSet<Auth>(registry.values());
    }

    public Player getPlayer(DomainPlayer domainPlayer)
    {
        Auth auth = registry.get(domainPlayer);
        if (auth==null) {
            throw new IllegalStateException("Session no found");
        }
        return new Player(domainPlayer.getNick(), auth);
    }

}
